package com.hsyt.pfc.redis.serializer;

import org.nustaq.serialization.FSTConfiguration;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * @Description:
 * @ClassName: FstRedisSerializer
 * @Package: com.hsyt.pfc.redis.serializer
 * @Author: wangze@hsyuntai.com
 * @Date: 2018年9月18日 15:51
 * @Copyright: 版权归 HSYUNTAI 所有
 */
public class FstRedisSerializer<T> implements RedisSerializer<T> {

    static FSTConfiguration configuration = FSTConfiguration.createDefaultConfiguration();

    public static Object unserialize(byte[] sec) {
        return configuration.asObject(sec);
    }

    @Override
    public byte[] serialize(T value) throws SerializationException {
        return configuration.asByteArray(value);
    }

    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null) {
            return null;
        }
        return (T) configuration.asObject(bytes);
    }
}
